<?php

/*******************************************************************************
 * 
 * 
 * 
 ******************************************************************************/


/**
 * 
 * openStandardIO()
 * 
 * @param int $stdin reference to a var that store the pointer to the opened stdin
 * @param int $stdout reference to a var that store the pointer to the opened stdout
 * 
 */
function openStandardIO(&$stdin=false,&$stdout=false)
{
	// Ouverture de stdin / stdout
	$stdin = fopen("php://stdin","w+b");
	$stdout = fopen("php://stdout","r+b");
	if (!$stdin) return false;
	if (!$stdout) return false;
}

/**
 * 
 * closeStandardIO()
 * 
 * @param int $stdin pointer to the opened stdin
 * @param int $stdout pointer to the opened stdout
 * 
 */

function closeStandardIO($stdin=false,$stdout=false)
{
	if ($stdin && $stdout)
	{
		return(@fclose($stdin) && @fclose($stdout));
	}
	else
	{
		return false;
	}
}

/**
 * 
 * writeStandardOutput()
 * 
 * 
 */
function writeStandardOutput($message='')
{
	$message = (is_array($message)) ? implode("\n",$message) : $message;
	$lMessage = mb_strlen($message);
	return @fwrite(STDOUT,$message);
}

/**
 * 
 * openStandardError()
 * 
 * 
 * 
 */
function openStandardError(&$stdrr=FALSE)
{
	if ($stderr)
	{
		return (! $stderr = @fopen("php://stderr",'w+b')) ? TRUE : FALSE;
	}
	
	return FALSE;
}

/**
 * 
 * writeStandardError()
 * 
 * 
 */
function writeStandardError($message='')
{
	$message = (is_array($message)) ? implode("\n",$message) : $message;
	$lMessage = mb_strlen($message);
	// return ($stderr) ? @fwrite($stderr,$message,$lMessage) : FALSE;
	return @fwrite(STDERR,$message);
}
/**
 * 
 * writeToSyslog()
 * 
 */
function writeToSyslog($message=false,$level=LOG_INFO)
{
	/*
	 * Syslog Levels :
	 * 
	 * LOG_EMERG  	système inutilisable
	 * LOG_ALERT 	une décision doit être prise immédiatement
	 * LOG_CRIT 	condition critique
	 * LOG_ERR 	    condition d'erreur
	 * LOG_WARNING 	condition d'alerte
	 * LOG_NOTICE 	condition normale, mais significative
	 * LOG_INFO 	message d'information
	 * LOG_DEBUG 	message de déboguage
	 * 
	 */
	
	if (!$message) return false;
	
	$message = (is_array($message)) ? implode("\n",$message) : $message;
	
	return @syslog($level,$message);
}

/**
 * 
 * dieWithErrorLevel()
 * 
 */
function dieWithErrorLevel($errorLevel=0)
{
	global $appErrors;
	global $appName;
	
	writeStandardError("$errorLevel : {$appErrors[$errorLevel]}\n");
	writeToSyslog("$appName error ($errorLevel) : {$appErrors[$errorLevel]}");
	die($errorLevel);
}


?>
